package com.lly.songhigh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.lly.songhigh.R;
import com.lly.songhigh.adapter.LocalMusicListAdapter;
import com.lly.songhigh.application.BaseApplication;
import com.lly.songhigh.constant.StringResouce;
import com.lly.songhigh.entity.LocalMusicInfo;
import com.lly.songhigh.entity.MusicInfo;
import com.lly.songhigh.entity.dao.RecentPlay;
import com.lly.songhigh.eventbus.MyEvent;
import com.lly.songhigh.greendao.DaoSingleton;
import com.lly.songhigh.utils.SharedPreferencesUtils;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by 李新伟 on 15/11/11.
 */
public class RecentPlayActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private DaoSingleton daoSingleton;
    private Toolbar local_music_tb;
    private ListView local_music_listView;
    private TextView local_music_play;
    private LocalMusicListAdapter adapter;
    private List<MusicInfo> musicInfos;
    private List<RecentPlay> recentPlays;
    private List<LocalMusicInfo> localMusicInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_local_music);
        initView();
        initData();
    }

    private void initData() {
        daoSingleton = DaoSingleton.getInstance();
        localMusicInfos = new ArrayList<>();
        recentPlays = daoSingleton.getRecentPlay();
        musicInfos = new ArrayList<>();
        SharedPreferencesUtils.putSharePre(this, "RecentPlay", "count", recentPlays.size());
        for (RecentPlay recentPlay : recentPlays) {
            LocalMusicInfo localMusicInfo = new LocalMusicInfo();
            localMusicInfo.setAlbum(recentPlay.getAlbum());
            localMusicInfo.setDuration(recentPlay.getDuration());
            localMusicInfo.setUrl(recentPlay.getPlayUrl());
            localMusicInfo.setAlbum_id(recentPlay.getAlbumId());
            localMusicInfo.setTitle(recentPlay.getSongName());
            localMusicInfo.setArtist(recentPlay.getSingerName());
            localMusicInfo.setId(recentPlay.getSongId());
            localMusicInfo.setResouce(recentPlay.getResouce());
            localMusicInfos.add(localMusicInfo);
        }
        adapter = new LocalMusicListAdapter(this, localMusicInfos);
        local_music_listView.setAdapter(adapter);
        for (LocalMusicInfo info : localMusicInfos) { // 通过遍历将本地音乐信息添加到播放集合中
            MusicInfo musicInfo = new MusicInfo();
            musicInfo.setResouce(info.getResouce());
            musicInfo.setSongName(info.getTitle());
            musicInfo.setSingerName(info.getArtist());
            musicInfo.setAlbum(info.getAlbum());
            musicInfo.setDuration(info.getDuration());
            musicInfo.setUrl(info.getUrl());
            musicInfos.add(musicInfo);
        }
    }

    private void initView() {
        local_music_listView = (ListView) findViewById(R.id.local_music_listView);
        local_music_play = (TextView) findViewById(R.id.local_music_play);
        local_music_play.setOnClickListener(this);
        local_music_tb = (Toolbar) findViewById(R.id.local_music_tb);
        local_music_tb.setTitle("最近播放");
        local_music_tb.setNavigationIcon(R.mipmap.toolbar_back);
        setSupportActionBar(local_music_tb);
        local_music_tb.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        local_music_listView.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(RecentPlayActivity.this, PlayMusicActivity.class);
        EventBus.getDefault().post(new MyEvent.DeliverData(musicInfos, musicInfos.get(0).getSongName()));
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        LocalMusicInfo localMusicInfo = (LocalMusicInfo) parent.getItemAtPosition(position);
        DaoSingleton.getInstance().getRecentPlayDao().insertOrReplace(new RecentPlay(localMusicInfo.getTitle(),
                localMusicInfo.getArtist(), localMusicInfo.getAlbum(), StringResouce.RESOUCE_LOCAL,
                localMusicInfo.getUrl(), localMusicInfo.getAlbum_id(), localMusicInfo.getDuration(),
                localMusicInfo.getId()));
        Intent intent = new Intent(RecentPlayActivity.this, PlayMusicActivity.class);

        EventBus.getDefault().post(new MyEvent.DeliverData(musicInfos, localMusicInfo.getTitle()));
        startActivity(intent);

    }
}
